package com.blockmeta.bbs.minemodule.activity;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.blockmeta.bbs.baselibrary.net.callback.Callback;
import com.blockmeta.bbs.baselibrary.utils.gson.GsonUtil;
import com.blockmeta.bbs.baselibrary.utils.loading.ProgressBarUtil;
import com.blockmeta.bbs.businesslibrary.base.BaseActivity;
import com.blockmeta.bbs.businesslibrary.constant.NetConstant;
import com.blockmeta.bbs.businesslibrary.net.okhttp.AdapterLoadDataBusiness;
import com.blockmeta.bbs.businesslibrary.net.okhttp.OKHttpGetReq;
import com.blockmeta.bbs.businesslibrary.pojo.ActiveJoinPOJO;
import com.blockmeta.bbs.businesslibrary.util.AccountHelper;
import com.blockmeta.bbs.minemodule.R;
import com.blockmeta.bbs.minemodule.adapter.ActiveJoinAdapter;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * Created by author on 2018/2/1.
 */

public class MyJoinActiveActivity extends BaseActivity implements OnRefreshListener, OnLoadmoreListener {
    private Context mContext;
    private SmartRefreshLayout list_swipe;
    private View view;
    private RecyclerView list_recy;
    private ActiveJoinPOJO pojo;
    private List<ActiveJoinPOJO> list;
    private ActiveJoinAdapter adapter;
    private int pageNo = 1;
    private OKHttpGetReq mGetReq;

    @Override
    protected boolean showTitle() {
        return true;
    }

    @Override
    public View setView() {
        mContext = MyJoinActiveActivity.this;
        setTitleText("申请活动列表");
        view = LayoutInflater.from(this).inflate(R.layout.fragment_list_smart, null);
        initData();
        return view;
    }

    private void initData() {
        list_swipe = (SmartRefreshLayout) view.findViewById(R.id.list_swipe);
        list_recy = (RecyclerView) view.findViewById(R.id.list_recy);
        list_swipe.setOnRefreshListener(this);
        list_swipe.setOnLoadmoreListener(this);
        list_swipe.setRefreshFooter(new ClassicsFooter(mContext));
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        list_recy.setLayoutManager(manager);
        adapter = new ActiveJoinAdapter(R.layout.item_list_active, null, mContext);
        list_recy.setAdapter(adapter);
        initNet("1", false);
    }

    private void initNet(String page, final boolean isLoadMore) {
        mGetReq = new OKHttpGetReq(mContext, NetConstant.SUBMIT_ACTIVE_FORM);
        HashMap<String, String> map = new HashMap<>();
        map.put("page", page);
        mGetReq.getDataWithParam(new Callback() {
            @Override
            public void onError(String resp) {
                processErrorData();
            }

            @Override
            public void onResponse(String resp, int id) {
                processData(resp, isLoadMore);
            }
        }, "uid", AccountHelper.getUid(), map);

    }

    private void processErrorData() {
        ProgressBarUtil.dismissProgress(mContext);
        View errorView = LayoutInflater.from(mContext).inflate(R.layout.empty_net, null);
        ImageView reconn = (ImageView) errorView.findViewById(R.id.click_reconn);
        reconn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initNet("1", false);
            }
        });
        adapter.setEmptyView(errorView);
    }

    private void processData(String resp, boolean isLoadMore) {
        AdapterLoadDataBusiness.finishLoadAndRefresh(list_swipe);
        view = LayoutInflater.from(mContext).inflate(R.layout.empty_data, null);
        adapter.setEmptyView(view);

        if (isLoadMore) {
            if (parseJson(resp) == null) {
                list_swipe.finishLoadmore();
            } else {
                adapter.addData(parseJson(resp));
            }
        } else {
            adapter.setNewData(parseJson(resp));
        }


    }

    private List<ActiveJoinPOJO> parseJson(String resp) {
        list = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(resp);
            Iterator<String> keys = jsonObject.keys();
            while (keys.hasNext()) {
                String key = keys.next();
                JSONObject jsonobj = jsonObject.optJSONObject(key);
                String commentNew = jsonobj.toString();
                Type collectionType = new TypeToken<ActiveJoinPOJO>() {
                }.getType();
                ActiveJoinPOJO commentPojo = GsonUtil.buildGson().fromJson(commentNew, collectionType);
                list.add(commentPojo);

            }
            return list;
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public void onRefresh(final RefreshLayout refreshlayout) {
        pageNo = 1;
        initNet("1", false);
    }


    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        pageNo++;
        initNet(String.valueOf(pageNo), true);
    }
}
